#import "OmUploadMomentsVC.h"
#import "OmUploadMomentsVCImagePickerView.h"
#import "NoveltyApiPhotosUploadModel.h"
@interface OmUploadMomentsVC ()
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *postButton;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) OmUploadMomentsVCImagePickerView *imagePickerView;
@end
@implementation OmUploadMomentsVC
#pragma mark - Event
- (void)onCancelButtonTap {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)onPostButtonTap {
    if ((self.imagePickerView.selectedPhotoArray.count + self.textView.text.length) <= 0) {
        [MBProgressHUD showInfoMessage:ASLocalizedString(@"请丰富发布内容")];
        return;
    }
    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"发布中..")];
    if (self.imagePickerView.selectedPhotoArray.count <= 0) {
        [self postMomentsWithImages:nil content:self.textView.text];
    } else {
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"uid"] = curUser.id;
        param[@"token"] = curUser.token;
        [PPNetworkHelper uploadImagesWithURL:[cyurl makeURLWithC:@"novelty_api" A:@"photos_upload"] parameters:param name:nil images:self.imagePickerView.selectedPhotoArray fileNames:nil imageScale:1 imageType:@"png" progress:^(NSProgress *progress) {
        } success:^(id responseObject) {
            if([[responseObject valueForKey:@"code"] intValue] == 1) {
                NSDictionary *dataDic = responseObject[@"data"];
                NSMutableArray<NSString *> *imageUrlArray = [NSMutableArray array];
                for (NSString *key in dataDic) {
                    NoveltyApiPhotosUploadModel *model = [NoveltyApiPhotosUploadModel mj_objectWithKeyValues:dataDic[key]];
                    [imageUrlArray addObject:model.img];
                }
                [self postMomentsWithImages:imageUrlArray content:self.textView.text];
            } else {
                [MBProgressHUD showTipMessageInView:[responseObject valueForKey:@"msg"]];
            }
        } failure:^(NSError *error) {
        }];
    }
}
#pragma mark - Method
- (void)postMomentsWithImages:(NSArray *)images content:(NSString *)content {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"publish_portal"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"uid"] = curUser.id;
    param[@"token"] = curUser.token;
    param[@"content"] = content;
    if (images) param[@"images"] = images;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showSuccessMessage:ASLocalizedString(@"发布成功")];
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUD];
    } hasCache:NO];
}
#pragma mark - ConstructionScript
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
- (void)configSelf {
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)allocateComponents {
    self.cancelButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onCancelButtonTap), UIControlEventTouchUpInside);
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelButton setTitle:ASLocalizedString(@"取消") forState:UIControlStateNormal];
    [self.view addSubview:self.cancelButton];
    self.postButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onPostButtonTap), UIControlEventTouchUpInside);
    self.postButton.fp_cornerRadius(4);
    [self.postButton setTitle:ASLocalizedString(@"发表") forState:UIControlStateNormal];
    self.postButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    self.postButton.backgroundColor = HexadecimalColor(@"#FA5A8C");
    [self.view addSubview:self.postButton];
    self.textView = [[UITextView alloc] init];
    self.textView.placeholder = ASLocalizedString(@"这一刻的想法");
    [self.view addSubview:self.textView];
    self.imagePickerView = [[OmUploadMomentsVCImagePickerView alloc] init];
    [self.view addSubview:self.imagePickerView];
}
- (void)layoutComponents {
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(14);
        make.top.equalTo(self.view).offset(30);
    }];
    [self.postButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-14);
        make.centerY.equalTo(self.cancelButton);
        make.height.equalTo(@(28));
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(24);
        make.right.equalTo(self.view).offset(-24);
        make.top.equalTo(self.postButton.mas_bottom).offset(24);
        make.height.equalTo(@(68));
    }];
    [self.imagePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(24);
        make.right.equalTo(self.view).offset(-24);
        make.top.equalTo(self.textView.mas_bottom).offset(24);
        make.bottom.equalTo(self.view);
    }];
}
@end
